<?php
/**
 * Created by PhpStorm.
 * User: 96572
 * Date: 2017/4/29
 * Time: 16:18
 */
namespace app\common;

use Yii;
use yii\web\Controller;
use app\modules\admin\models\Menu;
use yii\helpers\Url;

class CommonController extends Controller
{
    /**
     * 成功时的跳转页面
     * @param $title
     * @param $message
     * @param $links
     */
    public function success($title,$message,$links)
    {
        $this->layout = false;
        echo $this->render(
            '@admin/views/common/success',
            [
                'title'=>$title,
                'message'=>$message,
                'links'=>$links,
            ]
        );
        exit();
    }

    /**
     * 失败跳转页面
     * @param $title
     * @param $message
     * @param $links
     */
    public function error($title,$message,$links)
    {
        $this->layout = false;
        echo $this->render(
            '@admin/views/common/error',
            [
                'title'=>$title,
                'message'=>$message,
                'links'=>$links,
            ]
        );
        exit();
    }
    /**
     * 获得当前用户请求方法唯一标识，由模块id,控制器id，当前action的id组成，以下划线
     * 分隔，用于验证是否有权限访问
     * @return string
     */
    public static function getCurrentPermissionName()
    {
        return static::getRbacID(
            \Yii::$app->controller->module->id,
            \Yii::$app->controller->id,\Yii::$app->requestedAction->id
        );
    }
    /**
     * 根据传入的参数(路由)获得权限名称
     * @param $moduleID
     * @param $controllerID
     * @param $actionID
     * @return string
     */
    static public function getPermissionNameByRoute($moduleID,$controllerID,$actionID)
    {
        return strtolower($moduleID . '_' . $controllerID . '_' . $actionID);
    }

    public function beforeAction($action)
    {
        //  左侧菜单
        Yii::$app->view->params['leftMenu'] = Menu::getMenuTreeForLeftMenu();
        // 面包屑
        $menuID = Menu::find()->select(['menu_id'])->where([
            'menu_module_id'=>Yii::$app->controller->module->id,
            'menu_controller_id'=>Yii::$app->controller->id,
            'menu_action_id'=>Yii::$app->controller->action->id,
        ])->one();
        $this->getBreadcrumbs(Yii::$app->view->params['leftMenu'],$menuID['menu_id']);
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    private function getBreadcrumbs($menus,$menuID)
    {
        foreach ($menus as $index => $leftMenu) {
            if (in_array($menuID,$leftMenu['child_menu_id']))
            {
                Yii::$app->view->params['breadcrumbs'][] = [
                    'label' => $leftMenu['label'],
                    'url' => $leftMenu['url']
                ];
                $this->getBreadcrumbs($leftMenu['items'],$menuID);
            }
            if ($leftMenu['menu_is_lowest'] && $menuID == $leftMenu['menu_id'])
            {
                Yii::$app->view->params['breadcrumbs'][] = [
                    'label' => $leftMenu['label'],
                    'url' => $leftMenu['url']
                ];
                Yii::$app->view->title = $leftMenu['label'];
                break;
            }
        }
    }
}